﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using McePicasa.Properties;
using FlickrNet;

namespace McePicasa.mceFlickr
{
    /**
     * After authorization instance of this object can be dropped becouse user token will be stored in app.config
     */
    public class FlickrUser
    {
        private Flickr flickr;
        private string flickrFrob;
        private string userName;


        public FlickrUser()
        {
            //If the user is still authenticated then automatically auth. him
            if (Settings.Default.UserToken != "")
            {
                this.flickr = new Flickr(Settings.Default.flickrApiKey, Settings.Default.SharedSecret, Settings.Default.UserToken);
            }
            else
            {
                this.flickr = new Flickr(Settings.Default.flickrApiKey, Settings.Default.SharedSecret);
            }

        }

        public FlickrUser(string userName)
        {
            //If the user is still authenticated then automatically auth. him
            if (Settings.Default.UserToken != "")
            {
                this.flickr = new Flickr(Settings.Default.flickrApiKey, Settings.Default.SharedSecret, Settings.Default.UserToken);
            }
            else
            {
                this.flickr = new Flickr(Settings.Default.flickrApiKey, Settings.Default.SharedSecret);
            }
            this.userName = userName;
        }

        ///<summary>Run this method to begin user login (flickr web page). Authentication process is divided into 2 parts</summary>
        public void RequestUserAuthentication()
        {
            this.flickrFrob = this.flickr.AuthGetFrob();
            string flickrUrl = this.flickr.AuthCalcUrl(this.flickrFrob, AuthLevel.Write);
            System.Diagnostics.Process.Start(flickrUrl);
        }
        ///<summary>Run this method to complete user authentication</summary>
        public void CompleteUserAuthentication()
        {
            try
            {
                Auth auth = flickr.AuthGetToken(this.flickrFrob);
                this.flickr.AuthToken = auth.Token;
                Settings.Default.UserToken = auth.Token;
                Settings.Default.Save();
            }
            catch (FlickrException fe)
            {
                Application.Current.MessageBox("Sorry but there was problem during user authorization.","Authorization problem");
                Log.LogIt(fe);
            }
        }
        ///<summary>If You want to unauth. user just run this method. It will erease his token so he won't be able to run auth-needed functions</summary>
        public void UnAuthenticateUser()
        {
            Settings.Default.UserToken = "";
            Settings.Default.Save();
            this.flickr.AuthToken = "";

        }




        public string UserName
        {
            get { return userName; }
            set { userName = value; }
        }
    }
}
